//
//  CoolAnimation.m
//  CoolFramework
//
//  Created by Felix on 12-4-20.
//  Copyright (c) 2012年 FelixApp. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "CoolAnimation.h"
#import "MBProgressHUD.h"
#import "PTSProgressHUD.h"

#define WAITING_HUD_TAG 986487
#define BUBBLE_HUD_TAG  986488

#define imageView_width   120.0f
#define imageView_height  56.0f

@implementation CoolAnimation

+ (void)showWaitingViewWithGifInView:(UIView*)view
{
    [self showWaitingViewWithGifInView:view withText:@"正在努力加载中" afterDelay:0];
}

+ (void)showWaitingViewWithGifInView:(UIView*)view afterDelay:(NSTimeInterval)delay
{
    [self showWaitingViewWithGifInView:view withText:@"正在努力加载中" afterDelay:delay];
}

+ (void)showWaitingViewWithGifInView:(UIView*)view withText:(NSString*)text afterDelay:(NSTimeInterval)delay {
    UIImageView *imageView = [PTSProgressHUD shareView].gifImageView;
    NSLayoutConstraint* imageViewHeightConstraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:imageView_height];
    NSLayoutConstraint* imageViewWidthConstraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:imageView_width];
    if ([CoolDevice systemVersion] >= 8.) {
        imageViewHeightConstraint.active = YES;
        imageViewWidthConstraint.active = YES;
    } else {
        [imageView addConstraint:imageViewHeightConstraint];
        [imageView addConstraint:imageViewWidthConstraint];
    }
    
    [PTSProgressHUD setTitleColor:[UIColor colorWithHexString:LIGHT_GRAY_COLOR]];
    [PTSProgressHUD setBgColor:[UIColor colorWithHexString:APP_BACKGROUNDCOLOR]];
    [PTSProgressHUD setTitleSize:[SCAppAppearance defaultContentFontWithSize:15.0f]];
    
    if (delay > 0.0f) {
        [PTSProgressHUD shareView].status = PTSProgressStatusClickUIControl;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if ([PTSProgressHUD shareView].status < PTSProgressStatusWillShowHudView) {
                [PTSProgressHUD showWithGifImagePath:nil withTitle:text toView:view];
            }
        });
    } else {
        [PTSProgressHUD showWithGifImagePath:nil withTitle:text toView:view];
    }
}

+ (void)hideWaitingViewWithGifFromView:(UIView*)view
{
    [PTSProgressHUD hide];
}

/////////////////////////////

+ (void)showWaitingViewInView:(UIView*)view {
    [CoolAnimation showWaitingViewInView:view withText:@"正在加载" detailText:nil customView:nil];
}

+ (void)showWaitingViewInView:(UIView*)view color:(UIColor*)color {
    [CoolAnimation showWaitingViewInView:view withText:@"正在加载" detailText:nil customView:nil afterDelay:0.0 hideAfterDelay:-1. color:color];
}

+ (void)showWaitingViewInView:(UIView*)view afterDelay:(NSTimeInterval)delay {
    [CoolAnimation showWaitingViewInView:view withText:@"正在加载" detailText:nil customView:nil afterDelay:delay];
}

+ (void)hideWaitingViewFromView:(UIView*)view {
    //[MBProgressHUD hideAllHUDsForView:view animated:YES];
    [MBProgressHUD hideHUDForView:view animated:YES];
}

+ (void)showWaitingViewInView:(UIView*)view withText:(NSString*)text detailText:(NSString*)detailText customView:(UIView*)customView {
    [CoolAnimation showWaitingViewInView:view withText:text detailText:detailText customView:customView afterDelay:0.0];
}

+ (void)showWaitingViewInView:(UIView*)view withText:(NSString*)text detailText:(NSString*)detailText customView:(UIView*)customView afterDelay:(NSTimeInterval)delay {
    [CoolAnimation showWaitingViewInView:view withText:text detailText:detailText customView:customView afterDelay:delay hideAfterDelay:-1.];
}

+ (void)showWaitingViewInView:(UIView*)view withText:(NSString*)text detailText:(NSString*)detailText customView:(UIView*)customView color:(UIColor*)color {
    [CoolAnimation showWaitingViewInView:view withText:text detailText:detailText customView:customView afterDelay:0.0 hideAfterDelay:-1. color:color];
}

+ (void)showWaitingViewInView:(UIView*)view withText:(NSString*)text detailText:(NSString*)detailText customView:(UIView*)customView afterDelay:(NSTimeInterval)delay hideAfterDelay:(NSTimeInterval)hideDelay {
    [CoolAnimation showWaitingViewInView:view withText:text detailText:detailText customView:customView afterDelay:delay hideAfterDelay:hideDelay color:nil];
}

+ (void)showWaitingViewInView:(UIView*)view withText:(NSString*)text detailText:(NSString*)detailText customView:(UIView*)customView afterDelay:(NSTimeInterval)delay hideAfterDelay:(NSTimeInterval)hideDelay color:(UIColor*)color {
    [CoolAnimation showWaitingViewInView:view withText:text detailText:detailText customView:customView afterDelay:delay hideAfterDelay:hideDelay color:nil yOffset:0];
}

+ (void)showWaitingViewInView:(UIView*)view withText:(NSString*)text detailText:(NSString*)detailText customView:(UIView*)customView afterDelay:(NSTimeInterval)delay hideAfterDelay:(NSTimeInterval)hideDelay color:(UIColor*)color yOffset:(float)yOffset {
    
    MBProgressHUD *hud = (MBProgressHUD*)[view viewWithTag:WAITING_HUD_TAG];
    BOOL alreadyDisplay = YES;
    if (!hud) {
        alreadyDisplay = NO;
        hud = [[MBProgressHUD alloc] initWithView:view];
        hud.labelFont = [SCAppAppearance defaultContentFontWithSize:16.0f];
      hud.detailsLabelFont = [SCAppAppearance defaultContentFontWithSize:13.0f];
        hud.tag = WAITING_HUD_TAG;
        [view addSubview:hud];
    }
    if (text) {
      hud.labelText = text;
        if (detailText) {
            hud.square = YES;
          hud.detailsLabelText = detailText;
        }
    }
    if (customView) {
        hud.mode = MBProgressHUDModeCustomView;
        hud.customView = customView;
    }
//    if (color) {
//        hud.color = color;
//    }
    hud.removeFromSuperViewOnHide = YES;
    hud.yOffset = yOffset;
    if (!alreadyDisplay) {
        if (delay > 0.0) {
            [hud performSelector:@selector(show:) withObject:@(YES) afterDelay:delay];
        } else {
            [hud show:YES];
        }
    }
    if (hideDelay > 0) {
        [hud hide:YES afterDelay:hideDelay];
    }
}

+ (void)showBubbleMessage:(NSString*)message inView:(UIView*)view {
    if (!view) {
        return;
    }
    [CoolAnimation showBubbleMessage:message inView:view hideAfterDelay:2.0];
}

+ (void)showBubbleMessage:(NSString*)message inView:(UIView*)view xOffset:(float)xOffset yOffset:(float)yOffset {
    [CoolAnimation showBubbleMessage:message inView:view hideAfterDelay:2.0 customView:nil xOffset:xOffset yOffset:yOffset];
}

+ (void)showBubbleMessage:(NSString*)message inView:(UIView*)view hideAfterDelay:(NSTimeInterval)delay {
    [CoolAnimation showBubbleMessage:message inView:view hideAfterDelay:2.0 customView:nil xOffset:0.f yOffset:0.f];
}

+ (void)showBubbleMessage:(NSString*)message inView:(UIView*)view hideAfterDelay:(NSTimeInterval)delay customView:(UIView*)customView xOffset:(float)xOffset yOffset:(float)yOffset {
    MBProgressHUD *hud = (MBProgressHUD*)[view viewWithTag:BUBBLE_HUD_TAG];
    if (hud) {
        [hud hide:YES];
        [hud removeFromSuperview];
    }
    hud = [[MBProgressHUD alloc] initWithView:view];
    hud.tag = BUBBLE_HUD_TAG;
    hud.labelFont = [SCAppAppearance defaultContentFontWithSize:16.0f];
    hud.detailsLabelFont = [SCAppAppearance defaultContentFontWithSize:13.0f];
    [view addSubview:hud];
    if (customView) {
        hud.mode = MBProgressHUDModeCustomView;
        hud.customView = customView;
    } else {
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.0;
    }
    hud.labelText = message;
    hud.removeFromSuperViewOnHide = YES;
    hud.userInteractionEnabled = NO;
    hud.xOffset = xOffset;
    hud.yOffset = yOffset;
    [hud show:YES];
    if (delay > 0) {
        [hud hide:YES afterDelay:delay];
    }
}

+ (MBProgressHUD*)bubbleMessageHUDInView:(UIView*)view {
    return (MBProgressHUD*)[view viewWithTag:BUBBLE_HUD_TAG];
}

+ (void)hideBubbleMessageViewInView:(UIView*)view {
    MBProgressHUD *hud = (MBProgressHUD*)[view viewWithTag:BUBBLE_HUD_TAG];
    if (hud) {
        [hud hide:YES];
    }
}

+ (void)displayShadow:(BOOL)display ofView:(UIView*)view {
    [CoolAnimation displayShadow:display ofView:view shadowColor:[UIColor blackColor]];
}

+ (void)displayShadow:(BOOL)display ofView:(UIView*)view shadowColor:(UIColor*)shadowColor {
    [CoolAnimation displayShadow:display ofView:view shadowColor:shadowColor shadowRadius:3.0 shadowOpacity:0.9 shadowOffset:CGSizeMake(0.0, 0.0)];
}

+ (void)displayShadow:(BOOL)display ofView:(UIView*)view shadowColor:(UIColor*)shadowColor shadowRadius:(CGFloat)shadowRadius shadowOpacity:(CGFloat)shadowOpacity shadowOffset:(CGSize)shadowOffset {
    if (display) {
        view.layer.masksToBounds = NO;
        view.layer.shadowPath = [UIBezierPath bezierPathWithRect:view.layer.bounds].CGPath;
        view.layer.shadowColor = shadowColor.CGColor;
        view.layer.shadowOpacity = shadowOpacity;
        view.layer.shadowRadius = shadowRadius;
        view.layer.shadowOffset = shadowOffset;
    } else {
        view.layer.shadowColor = [UIColor clearColor].CGColor;
        view.layer.shadowOpacity = 0.0;
        view.layer.shadowRadius = 0.0;
        view.layer.shadowOffset = CGSizeMake(0.0, 0.0);
    }
}

+ (void)setNoContentViewInView:(UIView*)view hidden:(BOOL)hidden customText:(NSString*)customText frame:(CGRect)frame {
    NSInteger tag = 836583;
    UILabel *noContentLabel = (UILabel*)[view viewWithTag:tag];
    if (noContentLabel && ![noContentLabel isKindOfClass:[UILabel class]]) {
        LogError(@"noContentLabel is not a UILabel. %@", noContentLabel);
        return;
    }
    if (hidden) {
        [noContentLabel removeFromSuperview];
        noContentLabel = nil;
        return;
    }
    if (!noContentLabel) {
        noContentLabel = [[UILabel alloc] initWithFrame:view.bounds];
        noContentLabel.tag = tag;
        noContentLabel.backgroundColor = [UIColor clearColor];
        noContentLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        noContentLabel.font = [SCAppAppearance defaultContentFontWithSize:16.0f];//[UIFont boldSystemFontOfSize:16.0f];
        noContentLabel.textAlignment = NSTextAlignmentCenter;
        noContentLabel.textColor = [UIColor grayColor];
        noContentLabel.numberOfLines = 0;
    }
    [noContentLabel removeFromSuperview];
    noContentLabel.frame = frame;
    if (customText) {
        noContentLabel.text = customText;
    } else {
        noContentLabel.text = @"抱歉，目前暂无数据";
    }
    [view addSubview:noContentLabel];
}

+ (void)setNoContentViewInView:(UIView*)view hidden:(BOOL)hidden customText:(NSString*)customText {
    [self setNoContentViewInView:view hidden:hidden customText:customText frame:view.bounds];
}

+ (UIResponder*)firstResponderInView:(UIView*)view {
    if ([view isFirstResponder]) {
        return view;
    }
    NSArray *subViews = view.subviews;
    __block UIResponder *fr = nil;
    if (subViews.count > 0) {
        [subViews enumerateObjectsUsingBlock:^(UIView *subView, NSUInteger idx, BOOL *stop) {
            UIResponder *fr0 = [self firstResponderInView:subView];
            if (fr0) {
                fr = fr0;
                *stop = YES;
            }
        }];
    }
    return fr;
}

@end
